﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using diploma.core.data.subobjects;

namespace diploma.core.data.objects
{
    public class Property : DataObject
    {
        private List<PropertyValue> values;
        private long themeId;
        private long objectType;
        public long ThemeId
        {
            get
            {
                return themeId;
            }
        }
        public void SetThemeId(long _value)
        {
            themeId = _value;
            DataRouter.Instance.Act(DataActions.update, DataObjectTypes.Property, this);
        }

        public Property(long _id, string _title, long _themeId, long _objectType)
            : base(_id, _title)
        {
            values = new List<PropertyValue>();
            themeId = _themeId;
            objectType = _objectType;
        }

        public long ObjectType
        {
            get
            {
                return objectType;
            }
        }

        public string Title
        {
            get
            {
                return textField;
            }
        }

        public void SetTitle(string _value, bool _innerDataSourceChanges)
        {
            SetTextField(_value, _innerDataSourceChanges);
        }

        public void SetTitle(string _value)
        {
            SetTextField(_value);
        }
        
        public void setValues(IEnumerable<PropertyValue> _values)
        {
            values = new List<PropertyValue>(_values);
            DataRouter.Instance.Act(DataActions.update, this.Type, this);
        }

        public void addValue(PropertyValue _value)
        {
            values.Add(_value);
            DataRouter.Instance.Act(DataActions.update, this.Type, this);
        }

        public List<PropertyValue> Values
        {
            get
            {
                return values;
            }
        }

        public override DataObjectTypes Type
        {
            get
            {
                return DataObjectTypes.Property;
            }
        }

        #region ISerializable Members

        public override void FromStream(System.IO.Stream _stream)
        {
            base.FromStream(_stream);
            int n = ByteSerialization.IntFromBytes(_stream);
            for (int i = 0; i < n; i++)
            {
                PropertyValue tmp = new PropertyValue();
                tmp.FromStream(_stream);
                values.Add(tmp);
            }
        }

        public override void ToStream(System.IO.Stream _stream)
        {
            base.ToStream(_stream);
            ByteSerialization.IntToBytes(_stream, values.Count);
            foreach (PropertyValue _value in values)
            {
                _value.ToStream(_stream);
            }
        }

        #endregion

       
    }
}
